package org.example.test;

import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;

/**
 * @author: lynn
 * @Descript: 全排列
 * @date: 2022/3/13 22:51
 * @version: 1.0
 */
public class Permutation {
    List<List<Integer>> res=new ArrayList<>();
    public static void main(String[] args) {
        int[] nums=new int[]{1,2,3};
        Permutation p=new Permutation();
        System.out.println(p.permutation(nums));

    }

    public   List<List<Integer>> permutation(int[] nums){

        LinkedList<Integer> track=new LinkedList<>();

        backtrack(nums,track);
        return res;
    }

    void  backtrack(int[] nums,LinkedList<Integer> track){

        if(track.size()==nums.length){
            //res.add(track);
            res.add(new LinkedList<>(track));
            return;
        }

        for (int i = 0; i <nums.length ; i++) {
            if (track.contains(nums[i])){
                continue;
            }
            track.add(nums[i]);
            backtrack(nums,track);
            track.removeLast();
        }
    }
}
